// pages/ArticleDetails/ArticleDetails.js
var sayname = require("../../template/template.js");
var WxParse = require('../../wxParse/wxParse.js');
var menuEvent = require("../../utils/menuEvent.js").menuEvent;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasLoadImg:false,
    hqimage: app.hqimage.webUrl,
    menuarray: '',
    mItemID:'',
    Author:'',
    menuitem: {
      showmenu1: true,
      showmenu2: true,
      showmenu3: true,
      showmenu4: true,
      showmenu5: true

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    var that = this;
    
    

    wx.request({
      url: app.hqApi.webUrl +'/api/Article/GetArticleByKey',
      data: { KeyId: options.mItemID },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
     
        
        that.setData({
          mItemID: options.mItemID,
          Author: res.data.Author
        })
        
        var article = res.data.wArContent;
        if (article.length>=394714)
       {
        WxParse.wxParse('article', 'html', article.substr(0, article.length/2), that, 5);
        setTimeout(function () { WxParse.wxParse('article2', 'html', article.substr(article.length / 2, article.length ), that, 5)},1000);  
       }
       else{
          WxParse.wxParse('article', 'html', article.substr(0, article.length), that, 5);
       }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    setTimeout(function(){
     
      var hasLoadImg = that.data.hasLoadImg;
      if (hasLoadImg == false) {
       // console.log(that); 
        console.log(that.data.imgList)
        setHtmlImgShow(that, that.data.imgList);
        that.setData({
          hasLoadImg: true
        });
      }
     


    },1000);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.Author,
      desc: that.data.Author,
      path: '/pages/ArticleDetails/ArticleDetails?mItemID=' + that.data.mItemID
    }
  },
  templateEvent: function (e) {
    var that = this;
    sayname.GetMemu(that, e.currentTarget.dataset.event);//加载菜单数据
    menuEvent[e.currentTarget.dataset.event](e, that);

  },
  onPageScroll:function(){
    console.log("滚动");
    var that = this;
    var hasLoadImg = that.data.hasLoadImg;
    if (hasLoadImg == false) {
      console.log(that);
      setHtmlImgShow(that, that.data.imgList);
      that.setData({
        hasLoadImg: true
      });
    }

  },
  touchstart: function () {

  }
})


/*设置图片属性*/
function setHtmlImgShow(data, dom) {
  var param = {};
  var imgArray = new Array();
  if (dom != null && dom!=undefined && dom.length != undefined) {
    for (var index = 0; index < dom.length; index++) {
      imgArray.push(dom[index]);
    }
  }
  else {
    imgArray.push(dom);
  }
  for (var index = 0; index < imgArray.length; index++) {
    param[imgArray[index].nodeName + ".attr.src"] = imgArray[index].attr.dataSrc;
    console.log(param);
    data.setData(param);
    param = {};
  }
}